﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; 

public partial class management_Article_Article_List : System.Web.UI.Page
{
    private string title = HttpContext.Current.Request.QueryString["title"];
    private string mypath = "/userfiles/temp/default.jpg";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindDrpClass();
            Bind(); 
        }
    }

    //绑定顶级分类
    private void BindDrpClass()
    {
        int total = 0;
        DataTable dt = BLL.Article_Category.Categorys_Get(0, int.MaxValue, 0, out total); 
        DataRow[] drs = dt.Select("FId= " + 10);
        foreach (DataRow dr in drs)
        {
            string classid = dr["Id"].ToString();
            string classname = dr["Title"].ToString();
            //顶级分类显示形式
            classname = "╋" + classname;
            dropCategory.Items.Add(new ListItem(classname, classid));
            int sonparentid = int.Parse(classid);
            string blank = "├─";
            //递归子分类方法
            BindNode(sonparentid, dt, blank);
        }
        dropCategory.DataBind();
    }

    //绑定子分类
    private void BindNode(int parentid, DataTable dt, string blank)
    {
        DataRow[] drs = dt.Select("FId= " + parentid);
        foreach (DataRow dr in drs)
        {
            string classid = dr["Id"].ToString();
            string classname = dr["Title"].ToString();
            classname = blank + classname;
            dropCategory.Items.Add(new ListItem(classname, classid));
            int sonparentid = int.Parse(classid);
            string blank2 = blank + "─";
            BindNode(sonparentid, dt, blank2);
        }
    }

    //初始值绑定
    protected void Bind()
    {
        int CId = Convert.ToInt32(dropCategory.SelectedValue);
        int total = 0;
        BLL.Article.GetPageDT_Where(0, int.MaxValue, CId, out total);
        AspNetPager1.RecordCount = total;
        int StartRecord = AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1);
        int MaxRecord = AspNetPager1.PageSize;
        G1.DataSource = BLL.Article.GetPageDT_Where(StartRecord, MaxRecord, CId, out total);
        G1.DataBind();
         
    }
      

    //筛选删除
    protected void btn_delete_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < G1.Rows.Count; i++)
        {
            CheckBox myck = (CheckBox)G1.Rows[i].FindControl("Remove");
            if (myck.Checked)
            {
                int id = int.Parse(G1.DataKeys[i].Value.ToString());  
                BLL.Article.Delete(id);
            }
        }
        Bind();
    }

    //pager
    protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        Bind(); 
    }

    //行内删除事件
    protected void G1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(G1.DataKeys[e.RowIndex].Value.ToString());
        Label lblpic = (Label)G1.Rows[e.RowIndex].FindControl("lblpic");
        
        BLL.Article.Delete(id);
        Bind();
    }

    //分类下拉表改变时 数据绑定
    protected void dropCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind();
    }
     

    //置顶 Or 取消
    protected void G1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int Id = Convert.ToInt32(G1.DataKeys[e.RowIndex].Value);
        LinkButton btnUpTop = (LinkButton)G1.Rows[e.RowIndex].FindControl("btnUpTop");
        if (btnUpTop.Text == "置顶")
        {
            
        }
        else
        { 
        }
        Bind();
    }

    //在数据绑定后判断是显示置顶 Or 取消
    protected void G1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton btnUpTop = (LinkButton)e.Row.FindControl("btnUpTop");
             
        }
    }

    //鼠标移动变颜色
    protected void G1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.bgColor = '#FFEBCD';";
            e.Row.Attributes["onmouseout"] = "this.bgColor = '#FFFFFF';";
        }
    }
}
